<template>
  <div class="card content-box">
    <div class="table-box">
      <ProTable ref="proTable" :columns="columns" :request-api="getTableList">
        <!-- 表格 header 按钮 -->
        <template #tableHeader="scope">
          <el-button v-auth="'sys:sys_config:save'" type="primary" :icon="CirclePlus" @click="onSave">新增系统配置表</el-button>
          <el-button
            v-auth="'sys:sys_config:delete'"
            type="danger"
            :icon="Delete"
            plain
            :disabled="!scope.isSelected"
            @click="batchDelete(scope.selectedListIds)"
          >
            批量删除
          </el-button>
        </template>
        <!-- 菜单操作 -->
        <template #operation="scope">
          <el-button v-auth="'sys:sys_config:update'" type="primary" link :icon="EditPen" @click="onUpdate(scope.row)">
            编辑
          </el-button>
          <el-button v-auth="'sys:sys_config:delete'" type="danger" link :icon="Delete" @click="onDelete(scope.row)">
            删除
          </el-button>
        </template>
      </ProTable>
      <SysConfigOperateDrawer ref="drawerRef"></SysConfigOperateDrawer>
    </div>
  </div>
</template>

<script setup lang="ts" name="roleManage">
import { CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";
import ProTable from "@/components/ProTable/index.vue";
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface";
import {
  deleteSysConfig,
  getSysConfigPage,
  saveSysConfig,
  SysConfigPageResult,
  updateSysConfig
} from "@/api/modules/sys/SysConfig";
import { ref } from "vue";
import SysConfigOperateDrawer from "@/views/system/configManage/SysConfigOperateDrawer.vue";
import { useHandleData } from "@/hooks/useHandleData";
import { getEnumDictItem } from "@/api/modules/sys/dict";

// 表格配置项
const columns: ColumnProps[] = [
  { type: "selection", fixed: "left", width: 70 },
  {
    prop: "configKey",
    label: "配置key",
    search: { el: "input", tooltip: "配置key" }
  },
  {
    prop: "configValue",
    label: "配置值"
  },
  {
    prop: "category",
    label: "分类",
    enum: () => getEnumDictItem("SysConfigCategory"),
    search: { el: "select", tooltip: "分类" }
  },
  {
    prop: "remark",
    label: "备注"
  },
  {
    prop: "updateTime",
    label: "更新时间"
  },
  { prop: "operation", label: "操作", fixed: "right" }
];

const proTable = ref<ProTableInstance>();

const getTableList = param => {
  return getSysConfigPage(param);
};

const drawerRef = ref<InstanceType<typeof SysConfigOperateDrawer> | null>(null);

const onSave = () => {
  drawerRef.value?.openDrawer({
    title: "新增",
    isSave: true,
    isUpdate: false,
    row: {},
    api: saveSysConfig,
    getTableList: proTable.value?.getTableList,
    enumMap: proTable.value?.enumMap
  });
};

const onUpdate = (row: Partial<SysConfigPageResult> = {}) => {
  drawerRef.value?.openDrawer({
    title: "更新",
    isSave: false,
    isUpdate: true,
    row: { ...row },
    api: updateSysConfig,
    getTableList: proTable.value?.getTableList,
    enumMap: proTable.value?.enumMap
  });
};

// 删除信息
const onDelete = async (row: SysConfigPageResult) => {
  await useHandleData(deleteSysConfig, [row.id], `删除【${row.configKey}】`);
  proTable.value?.getTableList();
};

// 批量删除
const batchDelete = async (ids: string[]) => {
  await useHandleData(deleteSysConfig, ids, "删除所选信息");
  proTable.value?.clearSelection();
  proTable.value?.getTableList();
};
</script>
